package org.jeecg.modules.demo.cashier;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.mall.api.MallHelloApi;
import org.jeecg.modules.demo.mall.entity.YxStoreProduct;
import org.jeecg.modules.demo.weixin.entity.SmtOnceCardUsers;
import org.jeecg.modules.demo.weixin.smtoncecardusers.service.ISmtOnceCardUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description: 用户次卡表
 * @Author: jeecg-boot
 * @Date: 2023-10-28
 * @Version: V1.0
 */
@Api(tags = "用户次卡表")
@RestController
@RequestMapping("/weixin/cashieroncecardusers/cashierOnceCardUsers")
@Slf4j
public class CahierOnceCardUsersController {
    @Autowired
    private ISmtOnceCardUsersService smtOnceCardUsersService;


    @Autowired
    private MallHelloApi mallHelloApi;

    /**
     * 查询每个用户的已有计次项目
     *
     * @return
     */
    @GetMapping(value = "/queryList")
    public Result<?> queryList(@RequestParam("uid") Integer uid) {
        QueryWrapper<SmtOnceCardUsers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid);
        queryWrapper.gt("card_rest", 0);
        List<SmtOnceCardUsers> usersList = smtOnceCardUsersService.list(queryWrapper);
        return Result.OK(usersList);
    }


    /**
     * 查询每个用户的已有计次项目
     *
     * @return
     */
    /**
     * 分页列表查询
     *
     * @param smtOnceCardUsers
     * @param req
     * @return
     */
    @GetMapping(value = "/queryPage")
    public Result<List<SmtOnceCardUsers>> queryPageList(SmtOnceCardUsers smtOnceCardUsers,
                                                        HttpServletRequest req) {
        QueryWrapper<SmtOnceCardUsers> queryWrapper = new QueryWrapper<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        queryWrapper.eq("user_id", smtOnceCardUsers.getUserId());
        queryWrapper.eq("merchant_id", Integer.valueOf(smtOnceCardUsers.getMerId()));
        queryWrapper.gt("card_rest", 0);
        queryWrapper.orderByDesc("create_time");
        List<SmtOnceCardUsers> cardUsersList = smtOnceCardUsersService.list(queryWrapper);
        if (CollectionUtils.isEmpty(cardUsersList)) {
            return Result.ok();
        }
        List<SmtOnceCardUsers> counts = new ArrayList<>();
        Stream<SmtOnceCardUsers> smtOnceCardUsersStream = cardUsersList.stream().filter(smtOnceCardUsers1 ->
                smtOnceCardUsers1.getCardValidDate().equals("无限制")
        );
        if (smtOnceCardUsersStream != null) {
            List<SmtOnceCardUsers> onceCardUsers = smtOnceCardUsersStream.map(cardUser -> {
                if (cardUser.getGoodsId() != null) {
                    YxStoreProduct yxStoreProduct = mallHelloApi.queryName(Integer.valueOf(cardUser.getGoodsId()));
                    if (yxStoreProduct != null) {
                        cardUser.setGoodName(yxStoreProduct.getStoreName());
                    }
                }
                return cardUser;
            }).collect(Collectors.toList());
            counts.addAll(onceCardUsers);
        }
        cardUsersList.forEach(card -> {
            if (!card.getCardValidDate().equals("无限制")) {
                if (LocalDate.parse(card.getCardValidDate(), dateTimeFormatter).compareTo(LocalDate.now()) > 0) {
                    if (card.getGoodsId() != null) {
                        YxStoreProduct yxStoreProduct = mallHelloApi.queryName(Integer.valueOf(card.getGoodsId()));
                        if (yxStoreProduct != null) {
                            card.setGoodName(yxStoreProduct.getStoreName());
                        }
                    }

                    counts.add(card);
                }
                //次卡只是查询可用的  但是没有修改 如果要修改可在家字段   直接变成已过期
            }

        });
        return Result.OK(counts);
    }


    /**
     * 判断次卡是否购买
     *
     * @return
     */
    @GetMapping(value = "/queryUseCard")
    public Result<?> queryUseCard(@RequestParam Integer id, @RequestParam Integer userId, String merId) {
        QueryWrapper<SmtOnceCardUsers> queryWrapper = new QueryWrapper<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("merchant_id", Integer.valueOf(merId));
        queryWrapper.eq("card_code", id);
        queryWrapper.gt("card_rest", 0);
        queryWrapper.orderByDesc("create_time");
        List<SmtOnceCardUsers> cardUsersList = smtOnceCardUsersService.list(queryWrapper);

        if (CollectionUtils.isEmpty(cardUsersList)) {
            return Result.OK(false);
        }
        return Result.OK(true);
    }
}
